package dao;

import pojo.Department;
import utils.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 陈金平
 * @Description
 * @create 2020/11/19 11:56
 */
public class DepartmentDaoImpl implements DepartmentDao{
    @Override
    public List<Department> selectAllDepartments() {
        List<Department> departments =new ArrayList<>();
        String sql = "select * from department ";
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet resultSet = jdbcUtil.executeQuery(sql);
        try {
            while(resultSet.next()) {
                Department department = new Department();

                department.setDid(resultSet.getInt("did"));
                department.setDname(resultSet.getString("dname"));

                departments.add(department);
            }

            return departments;
        } catch (SQLException e) {
            e.printStackTrace();

            return null;
        }
        finally {
            jdbcUtil.close();
        }
    }

    @Override
    public Integer insertOneDepartment(Department department) {
        JDBCUtil jdbcUtil =new JDBCUtil();
        String sql = "insert into department(dname) values(?) ";

        Integer status = jdbcUtil.executeUpdate(sql,false,department.getDname());
        jdbcUtil.close();
        return status;
    }

    @Override
    public Integer updateOneDepartment(Department department) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "update department set dname=? where did=? ";
        Integer status = jdbcUtil.executeUpdate(sql,false,department.getDname(),department.getDid());
        jdbcUtil.close();
        return status;
    }

    @Override
    public Department selectDepartmentById(Integer did) {
        List<Department> departments =new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql="select * from department where did=? ";
        ResultSet resultSet = jdbcUtil.executeQuery(sql,did);
        try {
            while(resultSet.next()) {
                Department department = new Department();

                department.setDid(resultSet.getInt("did"));
                department.setDname(resultSet.getString("dname"));

                departments.add(department);
            }

            return departments.size()>0?departments.get(0):null;
        } catch (SQLException e) {
            e.printStackTrace();

            return null;
        }
        finally {
            jdbcUtil.close();
        }
    }

    @Override
    public Integer deleteAllDepartment(Integer did) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "delete from department where did=? ";
        Integer status = jdbcUtil.executeUpdate(sql,false,did);
        jdbcUtil.close();
        return status;
    }

    @Override
    public Integer deleteAllDepartments(List<Integer> dIds) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "delete from department where did in ";
        StringBuffer buffer = new StringBuffer();

        for (Integer dId : dIds) {
            if (buffer.length() > 0) {
                buffer.append(",");
            }

            buffer.append(dId);
        }

        sql += "(" + buffer.toString() + ")";

            System.out.println(sql);

        Integer status = jdbcUtil.executeUpdate(sql,false);

            jdbcUtil.close();

            return status;
    }
}
